package com.time.company.components.popup;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.time.company.R;
import com.time.company.base.BaseLvAdapter;
import com.time.company.base.BasePW;
import com.time.company.utils.ScreenUtils;
import com.yanzhenjie.nohttp.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Created by deng on 2016/8/16.
 * E-mail: tx16bing@163.com
 */
public class ChoosePW extends BasePW {

    private Context mContext;
    private int mType;

    private List<String> t;
    private ChooseAdapter chooseAdapter;
    private ListView listLv;

    private String curChoose;

    public ChoosePW(Context context, ViewGroup view, List<String> t, int type, String curChoose) {
        this.mContext = context;
        this.mType = type;
        this.curChoose = curChoose;
        if (t == null) {
            this.t = new ArrayList<>();
        } else {
            this.t = t;
        }
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = inflater.inflate(R.layout.pw_select_company, view, false);
        assignViews(mView);
        setContentView(mView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        if (this.t.size() > 4){
            setHeight(ScreenUtils.dip2px(230));
        }else {
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        setFocusable(true);
        setOutsideTouchable(true);
        this.setAnimationStyle(R.style.Pw_Bottom_Animation);
        ColorDrawable dw = new ColorDrawable(0x80000000);
        this.setBackgroundDrawable(dw);

    }

    private boolean isCharacter() {
        if (mType == mContext.getResources().getInteger(R.integer.pw_type_character)) {
            return true;
        }

        return false;
    }

    private boolean isScale() {
        if (mType == mContext.getResources().getInteger(R.integer.pw_type_scale)) {
            return true;
        }

        return false;
    }

    private void assignViews(View mView) {
        TextView tvTip = (TextView) mView.findViewById(R.id.tv_company_tip);
        listLv = (ListView) mView.findViewById(R.id.lv_company_tip);
        listLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = (String) chooseAdapter.getItem(position);
                if (!TextUtils.equals(item, curChoose)){
                    curChoose = item;
                    chooseAdapter.setChoose(curChoose);
                    chooseAdapter.notifyDataSetInvalidated();
                }

                if (null != onItemClickListener){
                    onItemClickListener.onItemClick(mType, item);
                    dismiss();
                }
            }
        });
        if (isCharacter()) {
            tvTip.setVisibility(View.VISIBLE);
            tvTip.setText("请选择企业性质");
        } else if (isScale()){
            tvTip.setText("请选择企业规模");
            tvTip.setVisibility(View.VISIBLE);
        } else {
            tvTip.setVisibility(View.GONE);
        }

        setAdapter();
    }

    private void setAdapter() {
        if (chooseAdapter == null) {
            chooseAdapter = new ChooseAdapter(mContext, t);
            chooseAdapter.setChoose(curChoose);
            listLv.setAdapter(chooseAdapter);
        } else {
            chooseAdapter.setChoose(curChoose);
            chooseAdapter.updateData(t);
        }
    }

    private class ChooseAdapter extends BaseLvAdapter<String>{

        private String mChoose;

        public ChooseAdapter(Context mContext, List<String> t) {
            super(mContext, t);
        }

        @Override
        public int getItemResourceId(int type) {
            return R.layout.item_choose;
        }

        @Override
        public View getItemView(int position, View convertView, ViewHolder holder, int type) {
            String t = (String) getItem(position);
            TextView tvName = holder.getView(R.id.tv_name);
            if (!TextUtils.isEmpty(t)){
                tvName.setText(t);
            }

            Logger.i("My choose " +  t);
            if (TextUtils.equals( t, this.mChoose)){
                convertView.setBackgroundResource(R.drawable.selector_white_x_bg);
            }else {
                convertView.setBackgroundResource(R.drawable.selector_white);
            }

            return convertView;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            int type = getItemViewType(position);
            ViewHolder holder;
            if (convertView == null){
                convertView = mInflater.inflate(getItemResourceId(type), parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }

            return getItemView(position, convertView, holder, type);
        }

        public void setChoose(String choose){
            this.mChoose = choose;
        }
    }

}
